%Checkerboard correlation length test script

sub_flag = 1; %subplots?

%V = checker(5,3,2);
V = checker(10,3,5);
[xc,lags] = xcov3(V);
levels = linspace(min(xc(:))*.95,max(xc(:))*.95,9);
subdim = fit_subplot(length(levels));
cmap = jet(length(levels));
figure, hold on;
colormap(cmap);
ax_lim=[];
for j=1:3
    ax_lim = [ax_lim -size(V,j) size(V,j)];
end
i=0;
for L=levels
    i=i+1;
    if sub_flag
        subplot(subdim(1),subdim(2),i);
        isoBinary3(xc,lags,[],L,cmap(i,:),0.5);
        title(['XCovCoeff = ' num2str(L)])
    else
        isoBinary3(xc,lags,[],L,cmap(i,:),0.5);
        colorbar
        caxis([levels(1) levels(end)])
        defaultPlot()
    end
        axis(ax_lim)
end

figure;
%Plot sliced version at origin
slice(lags{1},lags{2},lags{3},xc,0,0,0)
shading interp

figure;
%Plot source binary
isoBinary3(V);

figure;
%Plot xcov on x dimension only
xxc = xc(:,ceil(end/2),ceil(end/2));
plot(lags{1},xxc);
[peaks,locs] = findpeaks(xxc);
diff(locs)
%recovers lag length of 20 for repetition

%remove zero lag, sort on next lags
locs = locs(peaks<1);
peaks = peaks(peaks<1);
table = [lags{1}(locs)' peaks];
table = sortrows(table,-2);
%print lag table
fprintf(1,'%8s %8s\n','Lag','XCov')
fprintf(1,'%8.2g %8.2g\n',table')